<?php 

namespace Admin\Controller;
use Think\Controller;
use Admin\Common\CommonController;
//哪个控制器需要权限控制就加上权限继承


/**
* 问题控制类
*/
class QuestionController extends CommonController
{
	//展示所有问题
	public function index(){

		//问题分页
		$link = M();
		$sql_all ="select count(*) c from question where deleted='0'";
		$result =current($link->query($sql_all))['c'];
		// var_dump($result);
		$Page =new \Think\Page($result,5);
		$show =$Page->show();
		// var_dump($res_new);

		//循环分类
		
		$sql ="select * from category";
		$res = $link->query($sql);
		// var_dump($res);
		


		//循环问题
		$sql_q ="select q.*,u.username,c.cname from question q left join user u on q.uid=u.id left join category c on q.qid=c.id where q.deleted='0' limit {$Page->firstRow},{$Page->listRows}";
		$data = $link->query($sql_q);
		// var_dump($data);
		$this->assign('show',$show);//输出分页
		$this->assign('res',$res);//输出分类
		$this->assign('data',$data);//输出所有问题

		$this->display();
	}


	//********************查看问题
	public function browse(){
		if ($_GET) {
			// var_dump($_GET);
			U('index/index/question',array('id'=>$_GET['id']));
		}
	}



//************************编辑问题
	public function question_edit(){
		if ($_GET) {
			// var_dump($_GET['id']);
			U('index/index/question_edit',array('id'=>$_GET['id']));
		}

	}



//************************删除问题
	public function question_delete(){

		if ($_GET) {
			// var_dump($_GET['id']);
			$id =$_GET['id'];//获取要删除的用户的id
	   		$link = M();
	   		$sql = "update question set deleted ='1' where id={$_GET['id']}";
	   		$res = $link->execute($sql);
	   		$this->success('删除成功!',U('admin/question/index'));
			// U('index/question/index',array('id'=>$_GET['id']));
		}

	}


	//***************恢复删除的问题
	public function question_recover()
	{
		if ($_GET) {
			// var_dump($_GET);
	   		$id =$_GET['id'];//获取要修改密码的用户的id
	   		$link = M();
	   		$sql = "update question set deleted ='0' where id={$_GET['id']}";
	   		$res = $link->execute($sql);
	   		$this->success('恢复成功!',U('admin/question/question_delete_list'));
	   	}

	}



// ***************展示被删除的问题
	public function question_delete_list(){
		//分页
    	$link = M();
   		$sql_all ="select count(*) cn from question where deleted ='1'";
   		$res =current($link->query($sql_all))['cn'];
   		 $Page = new \Think\Page($res,5);
   		 $show =$Page->show();

    	
    	//将被删除的问题的数据输出到模板
   		$sql_user = "select q.*,c.cname,u.username from question q left join category c  on q.qid=c.id left join user u on q.uid=u.id where q.deleted ='1' order by time asc limit {$Page->firstRow},{$Page->listRows}";
   		// var_dump($sql_user);exit;
   		$data = $link->query($sql_user);
   		// var_dump($data);


   		$this->assign('show',$show);//将分页输出
   		$this->assign('data',$data);//将用户数据输出到模板
        $this->display();
	}






}

 ?>